<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * HITI AdminCP
 *
 * @package     HITI AdminCP Project
 * @since       Monday, October 21, 2013, 6:00 PM
 * @final
 * @category    Controllers
 * @see 		Positions
 * @author      PHUOCNGUYEN - HITI Dev Team
 * @copyright   Copyright (c) 2013, HITI Corp
 */

class Positions extends CI_Controller {
	var $q = '';
	var $limit = '';
	var $orderby = '';
	var $ordermode = '';
	var $added = '';
	var $updated = '';
	var $permissed = '';
	var $failed = '';
	var $name = '';
	var $uri_arr = array();
	var $uri_str = '';
	var $user_id = 0;

	public function __construct()
	{
		parent::__construct();
		$this->load->model('hiti_model', 'hiti', true);
		$this->hiti->load_config();
		if (check_rights() == false)
		{
			redirect();
		}
		$this->load->model($GLOBALS['var']['act'].'_model', 'Pos', true);
		$this->q = $this->input->get('q', true);
		$this->orderby = $this->input->get('orderby', true);
		$this->ordermode = $this->input->get('ordermode', true);
		$this->added = $this->input->get('added', true);
		$this->updated = $this->input->get('updated', true);
		$this->permissed = $this->input->get('permissed', true);
		$this->failed = $this->input->get('failed', true);
		$this->name = $this->input->get('name', true);
		$this->uri_arr = array(
			'deleted' => $GLOBALS['var']['deleted'],
			'q' => $this->q,
			'rowstart' => $GLOBALS['var']['rowstart'],
			'limit' => $this->limit,
			'orderby' => $this->orderby,
			'ordermode' => $this->ordermode
		);
		$this->uri_str = url_uri($this->uri_arr);
	}

	public function index()
	{
		/*
		* Xu ly du lieu
		*/
		$num_rows = $this->Pos->show_positions($this->uri_arr, true);
		$data = array(
			'q' => $this->q,
			'rowstart' => $GLOBALS['var']['rowstart'],
			'added' => $this->added,
			'updated' => $this->updated,
			'failed' => $this->failed,
			'name' => $this->name,
			'uri_str' => $this->uri_str,
			'url_str' => site_url('positions'),
			'positions' => array(),
			'action' => site_url($GLOBALS['var']['act'].'/process').$this->uri_str,
			'parts' => array()
		);
		$rows = $this->Pos->show_positions($this->uri_arr);
		if ($rows) $data['positions'] = $rows;
		$rows = $this->Pos->show_parts($this->uri_arr);
		if ($rows) $data['parts'] = $rows;
		/*
		* Hien thi
		*/
		$header = array(
			'title' => 'Vị trí phòng ban',
			'add_link' => 'javascript:;" class="updateLink',
			'search' => true,
			'page_list' => '',
			'datetime_picker' => false,
			'submit_btn' => false,
			'cat_list' => array(),
			'uri' => $this->uri_arr,
			'act' => $GLOBALS['var']['act'],
			'do' => $GLOBALS['var']['do'],
			'id' => $GLOBALS['var']['id'],
			'filter_cat' => $GLOBALS['var']['filter_cat']
		);
		$this->load->view('header', $header);
		$this->load->view($GLOBALS['var']['act'], $data);
		$this->load->view('footer');
	}

	public function process()
	{
		/*
		* Kiem tra POST method
		*/
		if(!$_POST)
		{
			redirect();
		}
		/*
		* Kiem tra token va tham so yeu cau
		*/
		$name_vn = $this->input->post('name_vn', true);
		if (!token_validation() || $name_vn == '')
		{
			redirect($GLOBALS['var']['act']);
		}
		/*
		* Them tham so url
		*/
		$this->uri_arr['name'] = str_replace('&', '', $name_vn);
		/*
		* Xu ly du lieu
		*/
		$id = $this->input->post('id', true);
		if ((!$id && !$GLOBALS['per']['add']) || ($id && !$GLOBALS['per']['edit']))
		{
			redirect($GLOBALS['var']['act']);
		}
		$table = $this->input->post('table', true);
		$data = array(
			'name_vn' => $name_vn,
			'date_added' => date(TIME_SQL),
            'date_modified' => date(TIME_SQL)
		);
		/*
		* Ghi du lieu
		*/
		if ($table == 'parts')
		{
			if ($this->Pos->process_part($data, $id))
			{
				if ($id > 0)
				{
					$this->uri_arr['updated'] = 2;
				}
				else
				{
					$this->uri_arr['added'] = 2;
				}
			}
			else
			{
				$this->uri_arr['failed'] = 2;
			}
		}
		else
		{
			if ($this->Pos->process_position($data, $id))
			{
				if ($id > 0)
				{
					$this->uri_arr['updated'] = 1;
				}
				else
				{
					$this->uri_arr['added'] = 1;
				}
			}
			else
			{
				$this->uri_arr['failed'] = 1;
			}
		}
		/*
		* Chuyen huong
		*/
		redirect($GLOBALS['var']['act'].url_uri($this->uri_arr));
	}

}

/* End of file positions.php */
/* Location: ./application/controllers/positions.php */
